//
//  MyCarSelectMenuButton.m
//  SecondPage
//
//  Created by xiong qi on 13-5-13.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "MyCarSelectMenuButton.h"
#import "../../HondaPro_2/aejoy/Categories/Extensions+Honda.h"

@implementation MyCarSelectMenuButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 30);
        
        
        [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"currentKey" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"currentValue" options:NSKeyValueObservingOptionNew context:nil];
        
        [self addTarget:self action:@selector(onTapSelectMenuButton:) forControlEvents:UIControlEventTouchUpInside];
        
        self.frame = self.frame;

    }
    return self;
}

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"frame"];
    [self removeObserver:self forKeyPath:@"currentKey"];
    [self removeObserver:self forKeyPath:@"currentValue"];
    
    SafeRelease(_currentKey);
    SafeRelease(_currentValue);
    
    SafeRelease(_contentArray);
    
    [super dealloc];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"frame"]) {
        [self setBackgroundImage:[[UIImage imageNamed:@"select_menu_button"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 30) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateNormal];
    }
    
    if ([keyPath isEqualToString:@"currentKey"]) {
        NSString * value = nil;
        if ([NSString valid:self.currentKey]) {
            for (id object in self.contentArray) {
                if ([NSDictionary validDictionary:object]) {
                    if ([[object objectForKey:@"key"] isEqualToString:self.currentKey]) {
                        value = [object objectForKey:@"value"];
                        break;
                    }
                }
            }
        }
        
        self.currentValue = value;
    }
    
    if ([keyPath isEqualToString:@"currentValue"]) {
        if ([NSString invalid:self.currentValue]) {
            [self setTitle:@"< 空 >" forState:UIControlStateNormal];
            [self setTitleColor:[UIColor colorWithRed:0.624  green:0.624  blue:0.624  alpha:1.0] forState:UIControlStateNormal];
        }
        else {
            [self setTitle:self.currentValue forState:UIControlStateNormal];
            [self setTitleColor:[UIColor colorWithRed:0.4  green:0.4  blue:0.4  alpha:1.0] forState:UIControlStateNormal];
        }
    }

}
- (void)onTapSelectMenuButton:(UIButton *)button {
    if ([NSArray invalidArray:self.contentArray]) {
        return;
    }
    [MyCarSelectMenuViewController sharedController].from = button;
    [MyCarSelectMenuViewController sharedController].enableNilKey = self.enableNilKey;
    [[MyCarSelectMenuViewController sharedController] reloadSelectMenuWithContentArray:self.contentArray key:self.currentKey value:self.currentValue];
    [[UIApplication sharedApplication].keyWindow.rootViewController popViewController:[MyCarSelectMenuViewController sharedController] from:self completion:^{
        self.currentKey = [MyCarSelectMenuViewController sharedController].currentKey;
        self.currentValue = [MyCarSelectMenuViewController sharedController].currentValue;
    }];
}


@end
